//
//  MLComposeController.swift
//  MLSina20160624
//
//  Created by manmanlang on 16/7/3.
//  Copyright © 2016年 manmanlang. All rights reserved.
//

import UIKit

class MLComposeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    func setupUI() {
        view.addSubview(composeTextView)
        view.addSubview(toolBarView)
        composeTextView.snp_makeConstraints { (make) in
            make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
        toolBarView.snp_makeConstraints { (make) in
            make.bottom.leading.trailing.equalTo(view)
            make.height.equalTo(44)
        }
        setupNavigation()
        ///使用通知的方式监听文本框变化 如果有值则让占位文字隐藏
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MLComposeController.TextViewTextDidChange), name: UITextViewTextDidChangeNotification, object: nil)
        
        //监听按钮的点击
        toolBarView.closure = { [weak self](type: ToolBarType) ->() in
            switch type{
            case .Picture:
                self?.selectImages()
            case .Mention:
                print("@")
            case .Trend:
                print("#")
            case .Emoticon:
                print("表情")
            case .Add:
                print("+")
                
            }
        }
        
    }
    
    //处理按钮点击事件
    func selectImages() {
        let picker = UIImagePickerController()
        picker.delegate = self
        presentViewController(picker, animated: true, completion: nil)
    }
    
    func TextViewTextDidChange() {
        
    }
    
    func setupNavigation() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(MLComposeController.cancel))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发布", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(self.compose))
        navigationItem.titleView = titleLabelView
        navigationItem.rightBarButtonItem?.enabled = false
    }
    
    @objc private func cancel() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func compose() {
        print("发布微博")
    }
    
    //标题文字
    private lazy var titleLabelView: UILabel = {
        //你的昵称
        let userName = MLUserAccountTool.sharedTool.userAccount?.screen_name ?? ""
        let titleString = "发微博\n\(userName)"
        ///富文本
        let attr = NSMutableAttributedString(string: titleString)
        let range = (titleString as NSString).rangeOfString(userName)
        attr.addAttributes([NSForegroundColorAttributeName:ThemeColor,NSFontAttributeName:UIFont.systemFontOfSize(13)], range: range)
        
        let label = UILabel()
        label.attributedText = attr
        label.numberOfLines = 0
        label.textAlignment = .Center
        label.sizeToFit()//注意要设置大小
        return label
    }()
    
    private lazy var composeTextView:MLCompose = {
        
        let labelView = MLCompose()
        
        labelView.delegate = self
        
        labelView.alwaysBounceVertical = true
        labelView.font = UIFont.systemFontOfSize(15)
        labelView.placeholder = "请输入微博内容:比较烦 比较烦 总觉得日子过得有一些极端 我想我还是不习惯 从没没无闻到有人喜欢"
        
        return labelView
    }()
    
    private lazy var toolBarView:MLToolBar = {
        let toolBarView = MLToolBar()
        
        return toolBarView

    }()
    
}

extension MLComposeController:UITextViewDelegate {
    func textViewDidChange(textView: UITextView) {
        navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
    func scrollViewDidScroll(scrollView: UIScrollView) {
        composeTextView.resignFirstResponder()
    }
}

extension MLComposeController:UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        composeTextView.backgroundColor = UIColor(patternImage: image)
        ///如果你实现了代理方法，那么系统将不会为你关闭控制器
        picker.dismissViewControllerAnimated(false, completion: nil)
    }
}
